const app = getApp();
const _user = require("../../utils/User.js")
const api = require("../../server/Api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null, //用户信息
    userCode: "", //核销码
    curPage: 1,
    pageSize: 100,
    hasMore: true,
    items: [], //总商品数
    emptyType: 2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userCode: options.userCode
    })
    this.queryOrderList(1, this.data.pageSize);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //下拉啊刷新
  onPullDownRefresh: function() {
    this.refreshData();
  },

  //上拉加载
  onReachBottom: function() {
    if (this.data.hasMore == false) return;
    // tt.showNavigationBarLoading();
    let page = this.data.curPage + 1;
    this.queryOrderList(page, this.data.pageSize);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  //刷新列表
  refreshData: function() {
    // tt.showNavigationBarLoading();
    var that = this;
    this.setData({
      hasMore: true,
    })
    this.queryOrderList(1, this.data.pageSize);
  },
  //分页查询订单
  queryOrderList: function(page, pageSize) {
    let that = this;
    let userBean = app.globalData.userBean;
    api.queryStoreVerificationOrderListByUserCode(app.globalData.userShopId[0].id, that.data.userCode).then(res => {
      //请求成功
      that.querySuccess(res, page)
      tt.stopPullDownRefresh();
      that.setData({
        emptyType: 2
      })
    }).catch(msg => {
      tt.stopPullDownRefresh();
      that.setData({
        emptyType: 101
      })
      var message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
    })
  },

  //查询成功
  querySuccess: function(res, curPage) {
    let that = this;
    //数据
    let records = res.records;
    let maxPage = parseInt(res.pages);
    var datas = curPage == 1 ? [] : this.data.items;
    if (records) {
      datas = datas.concat(records);
    }
    that.setData({
      hasMore: maxPage > curPage,
      items: datas,
      curPage: curPage,
    })
  },
  hexiao(e) {
    let that = this;
    let order = e.currentTarget.dataset.order;
    api.verificationOrder(order.code, app.globalData.userShopId[0].id).then(() => {
      //app.showToast("订单已核销")
      tt.showModal({
        title: '订单已核销',
        showCancel: false
      })
      that.refreshData()
    }).catch(msg => {
      let message = typeof msg == "string" ? msg : msg.message;
      app.showToast("核销失败!" + message);
    })
  },
  hexiaoAll(){
    let that = this;
    api.verificationAllOrder(that.data.userCode, app.globalData.userShopId[0].id).then(() => {
      tt.showModal({
        title: '一键核销成功',
        showCancel: false
      })
      that.refreshData()
    }).catch(msg => {
      let message = typeof msg == "string" ? msg : msg.message;
      app.showToast("一键核销失败!" + message);
    })
  }
})